package com.deli.controller;

import com.alibaba.fastjson.JSON;
import com.deli.entity.ResultDo;
import com.deli.entity.TestUserList;
import com.deli.service.AdminService;
import com.deli.service.TestUserListService;
import com.deli.utils.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 查出结果
 */
@RestController
public class TestUserListController {

    private static final Logger logger = LoggerFactory.getLogger(TestUserListController.class);
    @Autowired
    private TestUserListService testUserListService;
    @Autowired
    private AdminService adminService;

    /**
     * 新增答题用户
     * userName       用户姓名
     * mobilePhone    手机号
     * departmentname 末级部门名
     * testType       测试类型
     * userPosition   用户岗位
     * textfield      花名
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "user/add")
    public String useradd(@RequestBody TestUserList testUserList,
                          HttpServletRequest request) {
        ResultDo<String> resultDo = new ResultDo<>();

        try {
            String workCode = ParamUtil.checkParam(request.getParameter("workCode"), "workCode");
            //判断是否是管理员
            adminService.findByExemple(workCode);

            String userName = ParamUtil.checkParam(testUserList.getUserName(), "userName");
            String mobilePhone = ParamUtil.checkParam(testUserList.getMobilePhone(), "mobilePhone");
            String departmentname = ParamUtil.checkParam(testUserList.getDepartmentname(), "departmentname");
            Integer testType = Integer.parseInt(ParamUtil.checkParam(testUserList.getTestType(), "testType"));
            String userPosition = ParamUtil.checkParam(testUserList.getUserPosition(), "userPosition");
            String textfield = ParamUtil.checkParam(testUserList.getTextfield(), "anotherName");

            testUserListService.addUser(userName, mobilePhone, departmentname, testType, userPosition, textfield);
            resultDo.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            resultDo.setSuccess(false);
            resultDo.setErrorDescription("在添加用户时发生异常：" + e.getMessage());
        } finally {
            return JSON.toJSONString(resultDo);
        }
    }

    /**
     * 用户列表(后台)
     * pageNum 当前页
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "user/list")
    public String userlist(@RequestBody TestUserList testUserList, HttpServletRequest request) {
        ResultDo<String> resultDo = new ResultDo<>();

        try {
            String userName = testUserList.getUserName();
            String mobilePhone = testUserList.getMobilePhone();
            String departmentname = testUserList.getDepartmentname();
            Integer testType = testUserList.getTestType();
            String userPosition = testUserList.getUserPosition();
            String textfield = testUserList.getTextfield();

            String workCode = ParamUtil.checkParam(request.getParameter("workCode"), "workCode");
            //判断是否是管理员
            adminService.findByExemple(workCode);
            Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
            List<LinkedHashMap<String, Object>> dataList = testUserListService.getList(userName, mobilePhone, departmentname,
                    testType, userPosition, textfield, pageNum);
            int dataCount = testUserListService.getUserCount();
            resultDo.setTotaldataCount(dataCount);//总记录数量
            //总记录条数
            resultDo.setSuccess(true);
            resultDo.setData(JSON.toJSONString(dataList));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            resultDo.setSuccess(false);
            resultDo.setErrorDescription("查询用户列表：" + e.getMessage());
        } finally {
            return JSON.toJSONString(resultDo);
        }
    }

    /**
     * 批量导入用户
     *
     * @param file
     * @param request
     * @return
     */
    @RequestMapping(value = "user/fileAdd")
    public String characterAnalysisShow(@RequestParam(value = "file") MultipartFile file,
                                        HttpServletRequest request) {
        ResultDo<String> resultDo = new ResultDo<>();

        try {
            String workCode = ParamUtil.checkParam(request.getParameter("workCode"), "workCode");
           // 判断是否是管理员
            adminService.findByExemple(workCode);
            String fileName = file.getOriginalFilename();
            HashMap<String, Object> hashMap = testUserListService.batchImport(fileName, file);
            int repeat = Integer.parseInt(hashMap.get("repeat").toString());
            resultDo.setSuccess(true);
            resultDo.setRepeatCount(repeat);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            resultDo.setSuccess(false);
            resultDo.setErrorDescription("在添加用户时发生异常：" + e.getMessage());
        } finally {
            return JSON.toJSONString(resultDo);
        }
    }
}